/**
 * 
 */
package com.zhangjihao.utils;

import java.io.Serializable;

/**
 * 说明：
 * 
 * @author  zhangjihao(mailto:zhangjihao@sohu.com)
 * @version v1.0
 * 创建于：2020年4月26日 下午4:33:05
 */
public class Page implements Serializable {

	private static final long serialVersionUID = 1L;

	/**当前页码*/
	private int pageIndex;
	
	/**每页数量*/
	private int pageSize;
	
	/**总记录数*/
	private int total;
	
	/**地址*/
	private String url;

	
	public Page(){}
	
	public Page(int pageIndex) {
		super();
		this.pageIndex = pageIndex;
	}
	
	public Page(int pageIndex, int pageSize) {
		super();
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
	}
	
	public Page(int pageIndex, int pageSize, String url) {
		super();
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
		this.url = url;
	}


	/**
	 * 功能：11 / 2 = 5
	 *
	 * @return
	 */
	public int getPageTotal(){
		int mod = total % pageSize;
		int pageTotal = total / pageSize;
		if(mod >= 0){
			pageTotal++;
		}
		return pageTotal;
	}
	
	
	public int getPageIndex() {
		if(pageIndex == 0){
			this.pageIndex = 1;
		}
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageSize() {
		if(pageSize == 0){
			this.pageSize = 10;
		}
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	
	@Override
	public String toString() {
		return getStyleNormal();
	}

	
	public String getStyleNormal(){
		boolean hasParam = this.url != null && this.url.contains("?");
		StringBuilder result = new StringBuilder();
		result.append("<a href=\"").append(url).append(hasParam ? '&' : '?').append("pageIndex=").append(pageIndex-1).append("\">").append("上一页</a>&nbsp;&nbsp;");
		result.append("<a href=\"").append(url).append(hasParam ? '&' : '?').append("pageIndex=").append(pageIndex+1).append("\">").append("下一页</a>&nbsp;&nbsp;");
		return result.toString();
	}
	
	public String getStyleBootstrap(){
		return null;
	}
	
	public static void main(String[] args) {
		Page page = new Page(3, 10, "/list?minCapacal=2000001");
		System.out.println(page);
	}
}
